package com.cskaoyan.market.service.wx;


import com.cskaoyan.market.db.domain.*;
import com.cskaoyan.market.db.mapper.MarketCategoryMapper;
import com.cskaoyan.market.db.mapper.MarketGoodsMapper;
import com.cskaoyan.market.db.mapper.MarketKeywordMapper;
import com.cskaoyan.market.db.mapper.MarketSearchHistoryMapper;
import com.cskaoyan.market.vo.HistoryKeyWord;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class WxSearchServiceImpl implements WxSearchService {
    @Autowired
    MarketKeywordMapper marketKeywordMapper;
    @Autowired
    MarketSearchHistoryMapper marketSearchHistoryMapper;
    @Autowired
    SecurityManager securityManager;
    @Autowired
    MarketCategoryMapper marketCategoryMapper;
    @Autowired
    MarketGoodsMapper marketGoodsMapper;
//    @Autowired
//    HttpSession session;


    @Override
    public List<HistoryKeyWord> indexHistoryWord() {
        MarketSearchHistoryExample marketSearchHistoryExample = new MarketSearchHistoryExample();
        //hitoryKeyword表
        List<MarketSearchHistory> hitoryKeywordListRes = marketSearchHistoryMapper.selectByExample(marketSearchHistoryExample);
        List<HistoryKeyWord> resList = new ArrayList<>();
        for (int i = 0; i < hitoryKeywordListRes.size(); i++) {
            if (!hitoryKeywordListRes.get(i).getDeleted()) {
                String keyWord = hitoryKeywordListRes.get(i).getKeyword();
                HistoryKeyWord historyKeyWord = new HistoryKeyWord(keyWord);
                resList.add(historyKeyWord);
            }
        }
        return resList;


    }

    @Override
    public Map<String, List<MarketKeyword>> indexKeyWord() {
        MarketKeywordExample marketKeywordExample = new MarketKeywordExample();
        List<MarketKeyword> defaulMarketkeywordList = new ArrayList<>();
        Map<String, List<MarketKeyword>> keyWordMap = new HashMap<>();
        //hotKeyword表,查询
        List<MarketKeyword> keywordList = marketKeywordMapper.selectByExample(marketKeywordExample);
        List<MarketKeyword> hotKeywordList = new ArrayList<>();
        for (int i = 0; i < keywordList.size(); i++) {
            if (keywordList.get(i).getIsHot() && !keywordList.get(i).getDeleted()) {
                hotKeywordList.add(keywordList.get(i));
            }
            if (keywordList.get(i).getIsDefault()) {
                MarketKeyword defaulMarketkeyword = keywordList.get(i);
                defaulMarketkeywordList.add(defaulMarketkeyword);
            }

        }
        keyWordMap.put("defaultKeyword", defaulMarketkeywordList);
        keyWordMap.put("hotkeywordList", hotKeywordList);
        return keyWordMap;


    }


    @Override
    public List<String> helper(String keyword) {
        MarketKeywordExample marketKeywordExample = new MarketKeywordExample();
        MarketKeywordExample.Criteria criteria = marketKeywordExample.createCriteria();
        //历史搜索表更新
        LocalDateTime now = LocalDateTime.now();

        if (!StringUtils.isEmpty(keyword)) {
            criteria.andKeywordLike("%" + keyword + "%");
        }
        List<MarketKeyword> keywordHelper = marketKeywordMapper.selectByExampleSelective(marketKeywordExample);

        List<String> keywordHelperList = new ArrayList<>();
//        for (int i = 0; i < keywordHelper.size(); i++) {
//            keywordHelperList.add(keywordHelper.get(i).getKeyword());
//        }
        MarketGoodsExample marketGoodsExample=new MarketGoodsExample();
        MarketGoodsExample.Criteria criteria1 = marketGoodsExample.createCriteria();
        criteria1.andKeywordsLike("%"+keyword+"%");
      List<MarketGoods> marketGoodsList=marketGoodsMapper.selectByExample(marketGoodsExample);
        for (int i = 0; i <marketGoodsList.size() ; i++) {
            keywordHelperList.add(marketGoodsList.get(i).getName());

        }

        return keywordHelperList;

    }

    @Override
    public Integer clearhistory() {
        MarketSearchHistoryExample marketSearchHistoryExample = new MarketSearchHistoryExample();
        //先查后改
//        List<MarketSearchHistory> marketSearchHistories = marketSearchHistoryMapper.selectByExample(marketSearchHistoryExample);
        int count = 0;
        Session session = SecurityUtils.getSubject().getSession();
        MarketUser user = (MarketUser) session.getAttribute("user");
//        for (int i = 0; i < marketSearchHistories.size(); i++) {
//            MarketSearchHistoryExample.Criteria criteria = marketSearchHistoryExample.createCriteria();
//            criteria.andUserIdEqualTo(user.getId());
//            MarketSearchHistory marketSearchHistory = marketSearchHistories.get(i);
//            marketSearchHistory.setDeleted(true);
//            marketSearchHistoryMapper.updateByExample(marketSearchHistory, marketSearchHistoryExample);
//            count++;
//        }
        MarketSearchHistoryExample.Criteria criteria = marketSearchHistoryExample.createCriteria();
        criteria.andUserIdEqualTo(user.getId());
        List<MarketSearchHistory> marketSearchHistories = marketSearchHistoryMapper.selectByExample(marketSearchHistoryExample);
        for (int i = 0; i <marketSearchHistories.size() ; i++) {
            if (!marketSearchHistories.get(i).getDeleted()) {
                marketSearchHistories.get(i).setDeleted(true);
                MarketSearchHistoryExample marketSearchHistoryExample1=new MarketSearchHistoryExample();
                MarketSearchHistoryExample.Criteria criteria1= marketSearchHistoryExample1.createCriteria();
                criteria1.andIdEqualTo(marketSearchHistories.get(i).getId());
                criteria1.andUserIdEqualTo(user.getId());
                marketSearchHistoryMapper.updateByExample(marketSearchHistories.get(i), marketSearchHistoryExample1);
                count++;
            }
        }


        return count;
    }


}

